%% split ETOPO into countries and into connected countries in Europe
% compute ruggedness measure within 0.25 degree cells

clear
close all
clc

set(0,'DefaultFigureWindowStyle','docked','DefaultFigureVisible','on')

% load list of connected countries
folders;
country_list_connected;

% append the files
for country_n=1:Ncountries
    
    country_icc = char( countries_connected (country_n) );  % country ICC code
    country = icc2name( country_icc );           % country name

    if ~ispc()   %% we are in a mac

        % folder to save the outcome
        datafolder_LAC_country = [ datafolder_LAC,country_icc,'/'];

    else   %% we are in a pc   

        % folder to save the outcome
        datafolder_LAC_country = [ datafolder_LAC,country_icc,'\' ];

    end
    
    clc
    disp( ['Country: ',country] )    
    
    % load sedac
    file  = [ datafolder_LAC_country,'relief_etopo.mat'];
    temp = load(file); 
    
    % save temp file
    relief_etopo_temp{country_n} = temp.relief_etopo;
    for i=1:length(relief_etopo_temp{country_n})
        relief_etopo_temp{country_n}(i).country = country;
    end
    
end

%
i=1;
for country_n=1:Ncountries
    for j=1:length(  relief_etopo_temp{country_n} )
        relief_etopo(i).Geometry =  relief_etopo_temp{country_n}(j).Geometry;
        relief_etopo(i).X =  relief_etopo_temp{country_n}(j).X;
        relief_etopo(i).Y =  relief_etopo_temp{country_n}(j).Y;
        relief_etopo(i).centerX =  relief_etopo_temp{country_n}(j).centerX;
        relief_etopo(i).centerY =  relief_etopo_temp{country_n}(j).centerY;
        relief_etopo(i).av_alt =  relief_etopo_temp{country_n}(j).av_alt;
        relief_etopo(i).sd_alt =  relief_etopo_temp{country_n}(j).sd_alt;
        relief_etopo(i).rugged =  relief_etopo_temp{country_n}(j).rugged;
        relief_etopo(i).country =  relief_etopo_temp{country_n}(j).country;
        i=i+1;
    end
end

% save
if strcmp(country_names_connected(1,2), 'BO')
    datafolder_connected_countries =  './connected_countries_data2/'  ;
end

save([datafolder_connected_countries,'relief_etopo.mat'],'relief_etopo');
    